﻿package net.wangit.view.unit.component;

import java.util.Map;

import net.wangit.context.ActionContext;
import net.wangit.framework.config.DomainsSettings;
import net.wangit.framework.util.ResponseWriter;
import net.wangit.util.Checker;
import net.wangit.view.page.PageImpl;
import net.wangit.view.template.Template;
import net.wangit.view.template.TemplateFactory;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitWriter;


public class FramesetUnitWriter implements UnitWriter {

	private static final String WINDOW_TITLE = "window-title";
	private static final String FRAMES = "frames";

	public FramesetUnitWriter() {
	}

	public void writePage(ActionContext ac, PageImpl page) throws Exception {
		Map<String, String> attributes = page.getAttributes();
		attributes.put(FramesetUnitWriter.WINDOW_TITLE, page.getWindowTitle());
		attributes.put(FramesetUnitWriter.FRAMES, page.getUnitComponent()
				.getContent());
		String html = getFramesetTemplate(page).combine(attributes);
		ResponseWriter.write(html);
	}

	private static Template getFramesetTemplate(PageImpl page) {
		String template = page.getTemplate();
		if (Checker.isEmpty(template))
			template = DomainsSettings.getFramesetTemplate(page.getDomain());
		return TemplateFactory.getTemplate(template);
	}

	public String writeUnit(ActionContext ac, UnitComponent uc) {
		return null;
	}
}
